< previous page page_446 next page >

Page 446
Examples Filename: Emp2.frm
To illustrate this problem, check out the sample Emp2.frm form (see Figure 22.3).
8160-0446a.gif
Figure 22.3
This version of the 
Collection class
will introduce a potential problem.
When you click the Add Problem command button, you add an Employee object and a Form object to the module-level Collection object. Then, when you click the Display command button, you receive an error retrieving the Form object. Heres the code for the Add Problem command button:
9f31265bb40b592b1929e18a47ab28e5.gif
Private Sub cmdAdd_Click()
   Dim oEmp As Employee

   Set oEmp = New Employee
   With oEmp
      .EmpID = CLng(txtEmpID)
      .FirstName = txtFirst
      .LastName = txtLast
      .Salary = CCur(txtSalary)
   End With

   ' Add New Object to Module Level Collection
   mcolEmps.Add oEmp, CStr(oEmp.EmpID)

   ' Add the current form object to
   ' the Module Level Collection
   mcolEmps.Add Me, "99"
End Sub
This code first adds an instance of the Employee object, and then it adds a reference to the current form and assigns the key value 99 to that form in the collection. Both are entered into the collection with no problem; however, when you attempt to retrieve the element that has the key 99, youll receive an error. Heres the code for the Display command button:

 
< previous page page_446 next page >

If you like this book, buy it!